﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.Model;
using PPAP.BO;
using System.Data;

namespace PPAP.Web.Supplier
{
    public partial class Contrct_Person :PageBase
    {
        public string PID { set; get; }
        ContractPersonBO bo = new ContractPersonBO();
        PlantBO pbo = new PlantBO();
        LogBO lbo = new LogBO();
        UsersBO ubo = new UsersBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                if (Request.QueryString["PID"] != null)
                {
                    PPAP.Model.Plant p = pbo.GetModel(int.Parse(Request.QueryString["PID"].ToString()));
                    if (p != null)
                    {
                        lblMainBusiness.Text = p.MAINBUSINESS;
                        lblPlantName.Text = p.PLANTNAME;
                        Model.Entity.SUPPLIER su = new SupplierBO().GetModel(p.SUPPLIERID.ToString());
                        if (su != null)
                        {
                            lblSupplierCode.Text = su.SUPPLIERCODE;
                            lblSupplierName.Text = su.SUPPLIERNAME;
                        }
                    }
                    PID = Request.QueryString["PID"].ToString();
                    LoadData(Request.QueryString["PID"].ToString());
                }
                if (Request.QueryString["CID"] != null)
                {
                    LoadCon(Request.QueryString["CID"].ToString());
                }
                if (Request.QueryString["deid"] != null)
                {
                    dele(Request.QueryString["deid"].ToString());
                }
                //if (CurrentUser.UnitId != "0" && CurrentUser.UnitId != "99")
                //{
                //    GridView1.Columns[8].Visible = false;
                //    GridView1.Columns[10].Visible = false;
                //    fie.Visible = false;
                //}
                 
            }
        }

        private void LoadCon(string id)
        {
            ContractPerson p = bo.GetModel(int.Parse(Request.QueryString["CID"].ToString()));
            txtEmail.Text = p.EMAIL;
            txtFirstName.Text = p.Firstname;
            txtTelephone.Text = p.TELEPHONE;
            rbtfemale.Checked = p.GENDER.Trim() == "female" ? true : false;
            rbtInactive.Checked = p.STATUS.Trim() == "inactive" ? true : false;
            ddlPostition.SelectedValue = p.POSTITION;
            txtFamilyName.Text = p.NAME;
            rbtN.Checked = p.Ismanager == 1 ? false : true;
            rbtY.Checked = p.Ismanager == 1 ? true : false;
            txtEmail.Enabled = false;
            Button1.Text = "Save";
        }

        private void dele(string id)
        {
            int num = bo.Delete(int.Parse(id));
            lbo.WriteLog(CurrentUser.Id, "Delete person");
            if (num > 0)
                AlertRedirect("Delete success", "ContrctPerson.aspx?PID=" + Request.QueryString["PID"].ToString());
            else
                AlertRedirect("Delete failure", "ContrctPerson.aspx?PID=" + Request.QueryString["PID"].ToString());
        }

        private void LoadData(string id)
        {
            DataSet p = bo.GetList(" Plantid=" + id);
            if (p.Tables[0].Rows.Count == 0)
            {
                DataRow dr = p.Tables[0].NewRow();
                dr[1] = "No data";
                p.Tables[0].Rows.Add(dr);
                GridView1.Columns[7].Visible = false;
                GridView1.Columns[9].Visible = false;
            }
            else if (CurrentUser.UnitId == "99")
            {
                foreach (DataRow dr in p.Tables[0].Rows)
                {
                    if (dr["Firstname"].ToString().Trim() == "")  //如果供应商管理员信息不完整，则提示完善信息
                    {
                        ta.Visible = true;
                        //AlertMessage("Please click on the 'edit' button below to complete your information.");
                    }
                }
            }
            GridView1.DataSource = p;
            GridView1.DataBind();
        }


        ///// <summary>
        ///// 添加
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    if (txtName.Text.Trim() == ""||txtEmail.Text.Trim()=="")
        //    {
        //        AlertMessage("The text frame not is empty");
        //        return;
        //    }
        //    if (Request.QueryString["RID"] == null && Session["SupplierInfo"] == null)
        //    {
        //        AlertMessage("No SupplierInfo");
        //    }
        //    else
        //    {
        //        int num = 0;
        //        ContractPerson p = new ContractPerson();
        //        if(Request.QueryString["CID"]!=null)
        //         p = bo.GetModel(int.Parse(Request.QueryString["CID"].ToString()));
        //        p.EMAIL = txtEmail.Text;
        //        p.GENDER = rbtMale.Checked ? "male" : "female";
        //        p.NAME = txtName.Text;
        //        p.POSTITION = ddlPostition.SelectedValue;
        //        p.STATUS = rbtActive.Checked ? "active" : "inactive";
        //        p.TELEPHONE = txtTelephone.Text;
        //        p.SUPPLIERID=Request.QueryString["RID"]!=null?int.Parse(Request.QueryString["RID"].ToString()):int.Parse(Session["SupplierInfo"].ToString());
        //        if (Button1.Text == "Add")
        //        {
        //            p.Password = "123456";
        //            if (bo.GetList(" Email='" + p.EMAIL + "'").Tables[0].Rows.Count > 0)
        //            {
        //                AlertMessage("Email already exists");
        //                return;
        //            }
        //            num = bo.Add(p);
        //            if (num > 0)
        //            {
        //                lbo.WriteLog(CurrentUser.Name, "Add ContractPerson:"+p.NAME);
        //               // AlertMessage("Save success");
        //                if (Request.QueryString["ID"] != null || Session["SupplierInfo"] != null)
        //                {
        //                    LoadData(Request.QueryString["RID"] == null ? Session["SupplierInfo"].ToString() : Request.QueryString["RID"].ToString());
        //                }
        //            }
        //        }
        //        else if(Button1.Text=="Save")
        //        {
        //            num = bo.Update(p);
        //            if (num > 0)
        //            {
        //                lbo.WriteLog(CurrentUser.Name, "Edit ContractPerson:" + p.NAME);
        //                Button1.Text = "Add";
        //               // AlertMessage("Save success");
        //                if (Request.QueryString["ID"] != null || Session["SupplierInfo"] != null)
        //                {
        //                    LoadData(Request.QueryString["RID"] == null ? Session["SupplierInfo"].ToString() : Request.QueryString["RID"].ToString());
        //                }
        //            }
        //        }
        //        txtEmail.Text = "";
        //        txtName.Text = "";
        //        txtTelephone.Text = "";
        //    }
        //}

        public string GetPhone(object ico)
        {
            return "ico/"+ico.ToString().Trim()+".png";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           
                int num = 0;
                ContractPerson p = new ContractPerson();
                if (Request.QueryString["CID"] != null)
                    p = bo.GetModel(int.Parse(Request.QueryString["CID"].ToString()));
                p.EMAIL = CheckWord(txtEmail.Text.ToLower());
                p.GENDER = rbtMale.Checked ? "male" : "female";
                p.NAME = CheckWord(txtFamilyName.Text);
                p.Firstname = CheckWord(txtFirstName.Text);
                p.POSTITION = ddlPostition.SelectedValue;
                p.STATUS = rbtActive.Checked ? "active" : "inactive";
                p.TELEPHONE = CheckWord(txtTelephone.Text);
                p.Plantid = Request.QueryString["PID"] != null ? int.Parse(Request.QueryString["PID"].ToString()) : 0;
                p.Ismanager = rbtY.Checked ? 1 : 0;
                if (Button1.Text == "Add")
                {
                    p.Password = "123456";
                    //if (p.EMAIL != CurrentUser.Id)
                    //{
                        if (ubo.CheckEamil(p.EMAIL))
                        {
                            AlertMessage("Email already exists");
                            return;
                        }
                        p.Isuse = 1;
                    //}
                    //else
                    //{
                    //    p.Isuse = 0;
                    //}
                    num = bo.Add(p);
                    if (num > 0)
                    {
                        lbo.WriteLog(CurrentUser.Id, "Add ContractPerson:" + p.NAME);
                        // AlertMessage("Save success");
                        if (Request.QueryString["PID"] != null )
                        {
                            AlertRedirect("Save success", "ContrctPerson.aspx?PID="+Request.QueryString["PID"].ToString());
                            //AlertReLoad("Save success");
                            //LoadData(Request.QueryString["PID"] == null ? "0" : Request.QueryString["PID"].ToString());
                        }
                    }
                }
                else if (Button1.Text == "Save")
                {
                    num = bo.Update(p);
                    if (num > 0)
                    {
                        lbo.WriteLog(CurrentUser.Id, "Edit ContractPerson:" + p.NAME);
                        Button1.Text = "Add";
                        // AlertMessage("Save success");
                        if (Request.QueryString["PID"] != null )
                        {
                            AlertRedirect("Save success", "ContrctPerson.aspx?PID=" + Request.QueryString["PID"].ToString());
                            //AlertReLoad("Save success");
                           // LoadData(Request.QueryString["PID"] == null ? "0" : Request.QueryString["PID"].ToString());
                        }
                    }
                }
                txtEmail.Text = "";
                txtFamilyName.Text = "";
                txtTelephone.Text = "";
                txtFirstName.Text = "";
                txtEmail.Enabled = true;
           
        }
    }
}